package com.chain.BrotherExchange.viewmodel;

import android.app.Application;
import android.arch.lifecycle.AndroidViewModel;
import android.arch.lifecycle.MutableLiveData;
import android.support.annotation.NonNull;
import android.widget.ImageView;

import com.chain.BrotherExchange.C;
import com.chain.BrotherExchange.data.DataRepository;
import com.chain.BrotherExchange.data.Resource;
import com.chain.BrotherExchange.data.model.AuthenticationNumber;
import com.chain.BrotherExchange.data.model.BaseResponseModel;
import com.chain.BrotherExchange.data.model.UserIsAuthentication;
import com.chain.BrotherExchange.utils.Androids;

import java.util.HashMap;
import java.util.Map;

/**
 * 身份认证
 */
public class IDAuthenticationViewModel extends AndroidViewModel {

    private final int uid = Androids.LocalConfig.getUid();
    private final String token = Androids.LocalConfig.getToken();
    private final String region = Androids.LocalConfig.getRegion();
    private Map<Integer, String> mIntegerStringMap = new HashMap<>();

    private MutableLiveData<Resource<Integer>> mUpdateImageLiveData;
    private MutableLiveData<Resource<BaseResponseModel>> mSendMsmLiveData;
    private MutableLiveData<Resource<BaseResponseModel>> mLevel2CertificationLiveData;
    private MutableLiveData<Resource<String>> mRealNameAuthenticationLiveData;
    private MutableLiveData<Resource<AuthenticationNumber.DataBean>> mAuthenticationNumberLiveData;
    private MutableLiveData<Resource<UserIsAuthentication.DataBeanX.DataBean>> mIsAuthenticationLiveData;

    public IDAuthenticationViewModel(@NonNull Application application) {
        super(application);
        mSendMsmLiveData = new MutableLiveData<>();
        mUpdateImageLiveData = new MutableLiveData<>();
        mIsAuthenticationLiveData = new MutableLiveData<>();
        mAuthenticationNumberLiveData = new MutableLiveData<>();
        mRealNameAuthenticationLiveData = new MutableLiveData<>();
        mLevel2CertificationLiveData = new MutableLiveData<>();
    }

    /**
     * 发送短信
     */
    public MutableLiveData<Resource<BaseResponseModel>> sendMessage() {
        String mobilePhone = Androids.LocalConfig.getPhone();
        mSendMsmLiveData.setValue(Resource.loading());
        DataRepository.get().sendMessage(region, mobilePhone, C.API_REAL_NAME_AUTHENTICATION_TYPE, result -> {
            if (result.isSucceed()) {
                mSendMsmLiveData.setValue(Resource.success(result.getMsg()));
            } else {
                mSendMsmLiveData.setValue(Resource.error(result.getMsg()));
            }
        });

        return mSendMsmLiveData;
    }

    /**
     * 实名认证
     */
    public MutableLiveData<Resource<String>> realNameAuthentication(
            String name, String id, String code) {
        if (VerificationManage.verifyRealNameAuthentication(name, id) &&
                VerificationManage.verifyMessageVerifyCode(code)) {
            mRealNameAuthenticationLiveData.setValue(Resource.loading());
            DataRepository.get().realNamAuthentication(token, uid, id, name, code, result -> {
                if (result.isSucceed()) {
                    mRealNameAuthenticationLiveData.setValue(Resource.success(result.getMsg()));
                } else {
                    mRealNameAuthenticationLiveData.setValue(Resource.error(result.getMsg()));
                }
            });
        }

        return mRealNameAuthenticationLiveData;
    }

    /**
     * 实名认证次数
     */
    public MutableLiveData<Resource<AuthenticationNumber.DataBean>> authenticationNumber() {
        mAuthenticationNumberLiveData.setValue(Resource.loading());
        DataRepository.get().authenticationNumber(uid, result -> {
            if (result.isSucceed()) {
                mAuthenticationNumberLiveData.setValue(Resource.success(result.getData()));
            } else {
                mAuthenticationNumberLiveData.setValue(Resource.error(result.getMsg()));
            }
        });
        return mAuthenticationNumberLiveData;
    }

    /**
     * 是否实名认证
     */
    public MutableLiveData<Resource<UserIsAuthentication.DataBeanX.DataBean>> isAuthentication() {
         final int uid = Androids.LocalConfig.getUid();
         final String token = Androids.LocalConfig.getToken();
        mIsAuthenticationLiveData.setValue(Resource.loading());

        DataRepository.get().userIsAuthentication(uid, token, result -> {
            UserIsAuthentication.DataBeanX resultData = result.getData();
            if (result.isSucceed()) {
                mIsAuthenticationLiveData.setValue(Resource.success(resultData.getDataX()));
            } else if (result.isNotNetwork()) {
                mIsAuthenticationLiveData.setValue(Resource.notNetwork());
            } else if (result.isLogonFailure()) {
                mIsAuthenticationLiveData.setValue(Resource.tokenError(result.getMsg()));
            } else {
                mIsAuthenticationLiveData.setValue(Resource.error(result.getMsg()));
            }
        });
        return mIsAuthenticationLiveData;
    }

    /**
     * 二级认证
     */
    public MutableLiveData<Resource<BaseResponseModel>> level2Certification() {
        mLevel2CertificationLiveData.setValue(Resource.loading());
        DataRepository.get().level2Certification(uid, token, mIntegerStringMap.get(1),
                mIntegerStringMap.get(2), mIntegerStringMap.get(3), result -> {
            if (result.isSucceed()) {
                mLevel2CertificationLiveData.setValue(Resource.success(result.getMsg()));
            } else {
                mLevel2CertificationLiveData.setValue(Resource.error(result.getMsg()));
            }
        });

        return mLevel2CertificationLiveData;
    }

    public MutableLiveData<Resource<Integer>> uploadFile(ImageView imageView, int type) {
        String base64 = Androids.toBase64(imageView);
        mUpdateImageLiveData.setValue(Resource.loading());
        DataRepository.get().uploadFile(base64, result -> {
            if (result.isSucceed()) {
                String path = result.getData().getPath();
                mIntegerStringMap.put(type, path);
                mUpdateImageLiveData.setValue(Resource.error(result.getMsg()));
            } else {
                mUpdateImageLiveData.setValue(Resource.error(result.getMsg()));
            }
        });

        return mUpdateImageLiveData;
    }
}
